<?php
/**
 * AJAX Handler for Settings: Update - This handles the settings changes for a project from the edit page
 * 
 * I have made sure Libdebug is silent in AJAX. This is because it can intefere with the HTML/Javascript as Libdebug Appends after </html>.
 * If you have a problem and need to debug comment $_LIBDEBUG->silence(); (the framework must also be in debugging mode for this to work)
 */

    $_ECLIPSEMDE = array();
    
    // Disable Templating System
    $_ECLIPSEMDE['TEMPLATING'] = false;
    require_once('../../SiteIncludes.GLOBAL.php');
    
    // Tell libdebug to never print out error information - overrides debugging flags in fwork
    $_LIBDEBUG->silence();
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	header("HTTP/1.1 400 Bad Request");
	die($nok_caller);
    }
    
    // Ensure all variables needed are set
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($gVAR['alias']) !== false) {
	try {
	    $Project =& $ProjectStash->get($gVAR['alias']);
	    if($Project->userCanEdit($_LOGGEDINUSER->get_userid()) === true) {
		if(isset($pVAR['ib_text_newname']) === true && isset($pVAR['ta_description_new']) === true && isset($pVAR['ib_text_newimg']) === true) {
		    $safe_name = $sql_conx->real_escape_string($pVAR['ib_text_newname']);
		    $safe_desc = $sql_conx->real_escape_string($pVAR['ta_description_new']);
		    $safe_icon = $sql_conx->real_escape_string($pVAR['ib_text_newimg']);
		    $safe_visible = isset($pVAR['cb_visibility']) === true ? '1' : '0';
		    $query = $sql_conx->query("UPDATE `tool_project` SET `name`='{$safe_name}', `description`='{$safe_desc}', `icon_url`='{$safe_icon}', `visible`='{$safe_visible}' WHERE `pid`='{$Project->getID()}' LIMIT 1;") or die('ah!');
		    //die("UPDATE `tool_project` SET `name`='{$safe_name}' WHERE `pid`='{$Project->getID()}' LIMIT 1;");
		    if($query !== false) {
			$Project->updateWrite($_LOGGEDINUSER->get_userid(), ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::ChangeSettings : ProjectEditType::AdminChangeSettings), array());
			/*$query = $sql_conx->query("UPDATE `tool_project` SET  WHERE `pid`='{$Project->getID()}' LIMIT 1;");
			if($query !== false) {*/
			    die($ok_caller);
			/*} else {
			    header("HTTP/1.1 400 Bad Request");
			    die($nok_caller);
			}*/
		    } else {
			header("HTTP/1.1 400 Bad Request");
			die($nok_caller);
		    }
		} else {
		    header("HTTP/1.1 400 Bad Request");
		    die($nok_caller);
		}
	    } else {
		throw new InvalidProjectException();
	    }
	} catch(InvalidProjectException $e) {
	    header("HTTP/1.1 400 Bad Request");
	    die($nok_caller);
	}
    } else {
	header("HTTP/1.1 400 Bad Request");
	die($nok_caller);
    }
?>